import requests
from bs4 import BeautifulSoup

# 定义一个函数，用于获取书籍信息
def get_book_info(url):
    response = requests.get(url)
    soup = BeautifulSoup(response.text, 'html.parser')

    # 获取书籍标题
    title = soup.find('span', {'property': 'v:itemreviewed'}).text

    # 获取评分
    rating = soup.find('strong', {'class': 'll rating_num'}).text

    # 获取评论人数
    comment_count = soup.find('div', {'class': 'star'}).find_all('span')[-1].text

    return {
        'title': title,
        'rating': rating,
        'comment_count': comment_count
    }

# 主程序
def main():
    url_template = "https://book.douban.com/subject/{}/"

    # 循环获取多个书籍信息
    for i in range(1, 6):  # 假设我们要获取前5本书的信息
        url = url_template.format(i)
        book_info = get_book_info(url)
        print(book_info)

    # 使用条件语句过滤评分高于4.5的书籍
    books_above_4_5 = [book for book in books_info if float(book['rating']) > 4.5]
    print("评分高于4.5的书籍有：")
    for book in books_above_4_5:
        print(book)

if __name__ == "__main__":
    main()
